#include <jni.h>
#include <string>
#include "Properties.h"
#include "AnalysisFile.h"
#include "MicLoader.h"

#ifdef MTK_BOARD
#include "./mtk/MicThread.h"
#endif
#ifdef HISI_BOARD
#include "./hisi/MicThread.h"
#endif
#ifdef AMLOGIC_BOARD
#include "./amlogic//MicThread.h"
#endif

#define TV_HUI_CAI_MIC_DEFAULT_NUM 2

static std::string filePath = "/storage/emulated/0/recorder";
static const std::string wavFile = filePath + "/haier.wav";
static const std::string pcmFile = filePath + "/haier.pcm";

static std::string jStringToString(JNIEnv* env, jstring jStr) {
    const char* nativeString = env->GetStringUTFChars(jStr, nullptr);
    std::string result(nativeString);
    env->ReleaseStringUTFChars(jStr, nativeString);
    return result;
}

extern "C"
JNIEXPORT jboolean JNICALL
Java_com_haier_tv_mic_detect_manager_HaierMicManager_isMicOk(JNIEnv *env, jobject thiz) {
    // TODO: implement isMicOk()

    auto m_pAnalysisFile = new AnalysisFile();
    int micNum = 4; //Properties::instance()->getTvMicNum();
    int totalNum = micNum + TV_HUI_CAI_MIC_DEFAULT_NUM;
    std::vector <double> micValue;

    LOGD("Java_com_haier_tv_mic_detect_manager_HaierMicManager_isMicOk %d",micNum);
    m_pAnalysisFile->wav2Pcm(wavFile.c_str(),pcmFile.c_str());
    for (int i = 0; i< totalNum;i++){
        m_pAnalysisFile->departChannel(pcmFile.c_str(),i,totalNum);
    }

    for(int i=0;i<micNum;i++){
        char fileName[56] = {0};
        double value;
        sprintf(fileName,"%d.pcm",i);
        m_pAnalysisFile->fftChannel(fileName,6,value);
        micValue.push_back(value);
    }

    return m_pAnalysisFile->checkMic(micValue);
}
extern "C"
JNIEXPORT jint JNICALL
Java_com_haier_tv_mic_detect_manager_HaierMicManager_nativeGetMicNum(JNIEnv *env, jobject thiz) {
    // TODO: implement nativeGetMicNum()
    return Properties::instance()->getTvMicNum();
}
extern "C"
JNIEXPORT jstring JNICALL
Java_com_haier_tv_mic_detect_manager_HaierMicManager_nativeGetPropertyString(JNIEnv *env,
                                                                             jobject thiz,
                                                                             jstring key,
                                                                             jstring def) {
    // TODO: implement nativeGetPropertyString()
    char value[PROPERTY_VALUE_MAX] = {0};

    std::string sKey = jStringToString(env,key);
    std::string sDef = jStringToString(env,def);

    Properties::instance()->property_get(sKey.c_str(),value,sDef.c_str());

    return env->NewStringUTF(value);
}

extern "C"
JNIEXPORT jboolean JNICALL
Java_com_haier_tv_mic_detect_manager_HaierMicManager_nativeMicDeInit(JNIEnv *env, jobject thiz) {
    // TODO: implement nativeMicDeInit()

    if (MicThread::getInstance()->isMicThreadRunning() == false)
    {
        LOGE("MicThread isMicThreadRunning false");
        return MI_SUCCESS;
    }
    return MicThread::getInstance()->DeInit() == MI_SUCCESS ? JNI_TRUE:JNI_FALSE;
}
extern "C"
JNIEXPORT jboolean JNICALL
Java_com_haier_tv_mic_detect_manager_HaierMicManager_nativeMicInit(JNIEnv *env, jobject thiz,
                                                                   jint channel_num,
                                                                   jint sample_rate) {
    // TODO: implement nativeMicInit()

    if (MicThread::getInstance()->isMicThreadRunning()){
        LOGD("MicThread isMicThreadRunning true");
        return MI_SUCCESS;
    }

    return MicThread::getInstance()->Init(channel_num,sample_rate) == MI_SUCCESS ? JNI_TRUE:JNI_FALSE;
}